Rust Tree-sitter
Rust bindings to the Tree-sitter parsing library.
Basic Usage
First, create a parser:
use ;
let mut parser = new;
Add the cc
crate to your Cargo.toml
under [build-dependencies]
:
[]
="*"
Then, add a language as a dependency:
[]
= "0.22"
= "0.21"
To then use a language, you assign them to the parser.
parser.set_language.expect;
Now you can parse source code:
let source_code = "fn test() {}";
let mut tree = parser.parse.unwrap;
let root_node = tree.root_node;
assert_eq!;
assert_eq!;
assert_eq!;
Editing
Once you have a syntax tree, you can update it when your source code changes.
Passing in the previous edited tree makes parse
run much more quickly:
let new_source_code = "fn test(a: u32) {}";
tree.edit;
let new_tree = parser.parse;
Text Input
The source code to parse can be provided either as a string, a slice, a vector, or as a function that returns a slice. The text can be encoded as either UTF8 or UTF16:
// Store some source code in an array of lines.
let lines = &;
// Parse the source code using a custom callback. The callback is called
// with both a byte offset and a row/column offset.
let tree = parser.parse_with.unwrap;
assert_eq!;